<?php
/**
*
* ShopWindow Toolset
*
* Copyright (C) 2008 Digital Window Ltd.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*
*/


require_once('class.api_client.php');


/**
 * Encapsulate the corresponding API functionality
 *
 */
class api_product_list extends api_client
{
	var $sQuery 				= 	null;			// the keywords to search for
	var $bAdult	 				=	null;			// include adult products
	var $bHotPick		 		=	null; 	 		// only hot pick
	var $bIncludeTree		 	=	true; 	 		// show products from within children categories
	var $sSort 					=	'popularity';	// one of [az, za, hi, lo, popularity]
	var $iOffset 				=	0;				// the offset on the list
	var $iLimit 				=	8;				// the maximum number of products
	var $aActiveRefineByGroups 	=	array(); 		// active refine by groups and definitions
	
	var $iReturnCount 		 	= 	0;				// how many products in the resultset
	var $iTotalCount 		 	= 	0;				// how many products in total



	/**
	 * Gets a list of products
	 *
	 * @copyright	DigitalWindow
 	 * @author		Kostas Melas <kostas@digitalwindow.com>
 	 *
 	 * @param 	string 	 $oParams->sQuery
 	 * @param 	bool	 $oParams->bAdult
 	 * @param	bool	 $oParams->bHotPick
 	 * @param 	bool	 $oParams->bIncludeTree
 	 * @param 	string 	 $oParams->sSort
 	 * @param 	int		 $oParams->iOffset
 	 * @param 	int		 $oParams->iLimit
 	 * @param 	array	 $oParams->aActiveRefineByGroups
	 * 
	 * @return 	array 	 of products
	 */
	function getProductList($oParams)
	{	
		// sanity checks
		$this->sQuery = 		 utf8_encode( substr($oParams->sQuery, 0, 50) );
		$this->bAdult = 		 is_bool($oParams->bAdult) 			? $oParams->bAdult 			: $this->bAdult;
		$this->bHotPick = 		 is_bool($oParams->bHotPick) 		? $oParams->bHotPick 		: $this->bHotPick;
		$this->bIncludeTree = 	 is_bool($oParams->bIncludeTree) 	? $oParams->bIncludeTree 	: $this->bIncludeTree;
		$this->iOffset = 		 is_numeric($oParams->iOffset) 		? $oParams->iOffset		  	: $this->iOffset;
		$this->iLimit = 		 is_numeric($oParams->iLimit) 		? $oParams->iLimit 		  	: $this->iLimit;
		$this->aActiveRefineByGroups = is_array($oParams->aActiveRefineByGroups) ? $oParams->aActiveRefineByGroups  : $this->aActiveRefineByGroups;


		// sSort
		if ( empty($oParams->sSort) ) {
			$this->sSort = empty($this->sQuery) ? $this->sSort : 'relevancy';
		}
		else {
			$this->sSort = $oParams->sSort;
		}

        // format refine bys for soap call
        $aTmpActiveRefineByGroups = $this->aActiveRefineByGroups;
        foreach ($aTmpActiveRefineByGroups as $name => &$val) {
        	if ( isset($val->aRefineByDefinitions ) ) {
        		$val->oRefineByDefinition =
        				$val->aRefineByDefinitions;
        		unset($val->aRefineByDefinitions);
        	}
        }

        $aParams= array('sQuery' 				=> $this->sQuery,
        				'bAdult' 				=> $this->bAdult,
        				'bHotPick' 				=> $this->bHotPick,
        				'bIncludeTree' 			=> $this->bIncludeTree,
        				'sSort' 				=> $this->sSort,
        				'iOffset' 				=> $this->iOffset,
        				'iLimit' 				=> $this->iLimit,
        				'oActiveRefineByGroup'	=> $aTmpActiveRefineByGroups );


       	// make the SOAP call
        $this->call('getProductList', $aParams);

		$aProducts = $this->oResponse->oProduct;

		$this->iReturnCount 			= count($aProducts);
		$this->iTotalCount 				= $this->oResponse->iTotalCount;
		$this->aActiveRefineByGroups 	= $this->oResponse->oActiveRefineByGroup;
		$this->aRefineByGroups 			= $this->oResponse->oRefineByGroup;
		
		// check not empty
		if ( !empty( $this->aActiveRefineByGroups ) ) {
			# VERTICALS
			foreach ( $this->aActiveRefineByGroups as $key => $oRefineByGroup ) {
				// pop the enforced category
				if ( $oRefineByGroup->iId == RB_CATEGORY && 
						VERTICAL_CATEGORY_ID == $oRefineByGroup->oRefineByDefinition[0]->sId ) {
					unset($this->aActiveRefineByGroups[$key]);
					break;
				}
			}
		}
		

		# FILTER CATEGORIES
		# emulate category objects from RBs, so we can apply the category filtering
		
		// check not empty
		if ( !empty( $this->aActiveRefineByGroups ) ) {
			// active RBs
			foreach ( $this->aActiveRefineByGroups as $oRefineByGroup ) {
				// category RB
				// set the iId attribute
				$oRefineByGroup->oRefineByDefinition[0]->iId =& intval($oRefineByGroup->oRefineByDefinition[0]->sId);

				// rename, remove
	    		$oRefineByGroup->aRefineByDefinitions = $this->filterCategories($oRefineByGroup->oRefineByDefinition);

	    		// unset it again
	    		unset($oRefineByGroup->aRefineByDefinitions[0]->iId);
				
				unset($oRefineByGroup->oRefineByDefinition);
			}
		}
		
				
		
				
		
		


		// check not empty
		if ( !empty( $this->aRefineByGroups ) ) {
			// available RBs
			foreach ( $this->aRefineByGroups as $oRefineByGroup ) {
				// category RB
				if ( $oRefineByGroup->iId == RB_CATEGORY ) {
					// set the iId attribute
					foreach ( $oRefineByGroup->oRefineByDefinition as $oRefineByDefinition ) {
						$oRefineByDefinition->iId =& intval($oRefineByDefinition->sId);
					}
					
					$oRefineByGroup->oRefineByDefinition = $this->filterCategories( $oRefineByGroup->oRefineByDefinition );
				}
				
				// update variable types from soap objects to array
				if (isset($oRefineByGroup->oRefineByDefinition)) {
					$oRefineByGroup->aRefineByDefinitions = $oRefineByGroup->oRefineByDefinition;
					unset($oRefineByGroup->oRefineByDefinition);
				}
			}
		}

        return $aProducts;
	}

	
} // class


?>